package gruppe7.db;

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBAccess {

    private static String CONTEXT = "java:comp/env";
    private static String CONTEXT_KEY = "jdbc/DB";
    private DataSource dataSource;
    private static DBAccess instance = null;

    public static DBAccess getInstance() throws Exception {
        if (instance == null) {
            instance = new DBAccess();
        }

        return instance;
    }

    private DBAccess() throws Exception {
        Context initial = new InitialContext();
        Context ctx = (Context) initial.lookup(CONTEXT);

        dataSource = (DataSource) ctx.lookup(CONTEXT_KEY);
    }

    public Connection getConnection() throws Exception {
        return dataSource.getConnection();
    }
}
